package cn.zlp.basic.service.impl;

import cn.zlp.basic.mapper.BaseMapper;
import cn.zlp.basic.query.BaseQuery;
import cn.zlp.basic.service.IBaseService;
import cn.zlp.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 抽取公共的service实现方法
 * @param <T>
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired(required = false)
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> pageQuery(BaseQuery baseQuery) {
        Long totals = baseMapper.getTotals(baseQuery);
        if (totals == null || totals < 1){
            return new PageList<>();
        }
        List<T> list = baseMapper.loadByQuery(baseQuery);
        return new PageList<>(totals,list);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }
}
